<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    public $data;
    public $httpcode = [
        'errno' => 200,
        'error' => '连接成功'
    ];

    public function __construct() {
        parent::__construct();
        $this->data['deparment'] = $this->config_model->getConfig('su_department');
    }

    public function login() {
        $this->load->view('public/header');
        $this->load->view('pages/login');
    }

    public function register() {
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/register');
    }

    public function register_do() {
        $redata = $this->httpcode;
        $username = $this->input->post('username');
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $repassword = $this->input->post('repassword');
        $department_id = $this->input->post('department');
        $register_time = tnow();
        $register_ip = getIp();
        if (verify_username($username) && verify_password($password) && verify_password($repassword) && verify_email($email) && is_numeric($department_id)) {
            if ($this->user_model->userIsExist($username, $email)) {
                $redata['errno'] = '602';
                $redata['error'] = '账号或邮箱已经被占用';
            } else {
                if ($password == $repassword) {
                    $department_id != 0 ? $level = 1 : $level = 0;
                    $department_id != 0 ? $is_verify = 0 : $is_verify = 1;
                    $data = [
                        'username' => $username,
                        'email' => $email,
                        'password' => md5('su' . $password),
                        'department_id' => $department_id,
                        'register_time' => $register_time,
                        'register_ip' => $register_ip,
                        'level' => $level,
                        'is_verify' => $is_verify
                    ];

                    if ($this->user_model->userRegister($data)) {
                        if ($is_verify == 0) {
                            $redata['errno'] = '201';
                            $redata['error'] = '注册成功,请等待部门管理员审核';
                        } else {
                            $redata['errno'] = '200';
                            $redata['error'] = '注册成功';
                        }
                    } else {
                        $redata['errno'] = '603';
                        $redata['error'] = '注册失败';
                    }
                } else {
                    $redata['errno'] = '601';
                    $redata['error'] = '输入的密码不一致';
                }
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '数据格式错误';
        }
        echo json_encode($redata);
    }

    public function login_do() {
        $redata = $this->httpcode;
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $login_time = tnow();
        $login_ip = getIp();
        $data = [
            'username' => $username,
            'password' => md5('su' . $password),
            'login_time' => $login_time,
            'login_ip' => $login_ip
        ];
        if (verify_username($username) && verify_password($password)) {
            $userInfo = $this->user_model->userLogin($data);
            if (!empty($userInfo)) {
                unset($userInfo['password']);
                $this->session->set_userdata($userInfo); //存储用户session数据
                $redata['errno'] = '200';
                $redata['error'] = '登录成功';
            } else {
                $redata['errno'] = '601';
                $redata['error'] = '账号或密码错误';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '数据格式错误';
        }
        echo json_encode($redata);
    }

    public function reset_password() {
        $redata = $this->httpcode;
        $password = $this->input->post('password');
        $repassword = $this->input->post('repassword');
        if (verify_password($password) && verify_password($repassword) && $repassword == $password) {
            $table = 'su_user';
            $where = [
                'id' => $this->session->id
            ];
            $data = [
                'password' => md5('su' . $password)
            ];
            if ($this->user_model->updateUserInfo($table, $where, $data)) {
                $redata['errno'] = 666;
                $redata['error'] = '密码修改成功,请重新登录';
                $this->session->sess_destroy();
            } else {
                $redata['errno'] = 600;
                $redata['error'] = '密码修改失败';
            }
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '密码格式错误';
        }
        echo json_encode($redata);
    }

    public function send_verifycode() {
        $this->load->model('common_model');
        $this->load->helper('cookie');
        $redata = $this->httpcode;
        $email = $this->input->post('email');
        $isemail = verify_email($email);
        $isset = $this->common_model->getList('su_user', ['email' => $email]);
        if ($isemail) {
            if (!empty($isset)) {
                $this->load->library('phpmailer/PHPMailer.php');
                $this->load->library('phpmailer/POP3.php');
                $this->load->library('phpmailer/SMTP.php');
                $mail = new PHPMailer();
                $mail->SMTPDebug = 1;
                $verifycode = create_verify_code();
                $subject = 'EmailSu密码重置通知';
                $message = '您好,验证码:' . $verifycode . ',有效期5分钟,如非本人操作,请忽略此邮件!过期时间' . date('Y-m-d H:i:s');
                $mail->isSMTP();
                $mail->SMTPAuth = TRUE;
                $mail->Host = 'smtp.qq.com';
                $mail->SMTPSecure = "ssl"; // 使用ssl协议方式
                $mail->Port = 465;
                $mail->CharSet = 'utf-8';
                $mail->Username = '1021840359';
                $mail->Password = 'jwmumsvdegwxbede';
                $mail->From = 'admin@3fengtech.com';
                $mail->FromName = 'EmailSu管理员'; //发送人昵称
                
                $mail->isHTML(true); //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
                $mail->addAddress($email);
                $mail->Subject = $subject; //添加该邮件的主题
                $mail->Body = $message; //添加邮件正文 
                $state = $mail->send(); //发送命令 返回布尔值 
                $mail->smtpClose();
                echo $mail->ErrorInfo;
                if ($state) {
                    set_cookie("{$message}", "{$verifycode}", 300);
                    $redata['errno'] = 666;
                    $redata['error'] = '验证码已经发送到邮箱,有效期五分钟!';
                } else {
                    $redata['errno'] = 602;
                    $redata['error'] = '验证码发送失败';
                }
            } else {
                $redata['errno'] = 601;
                $redata['error'] = '该邮箱还没有注册';
            }
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '邮箱格式错误';
        }
        echo json_encode($redata);
    }

    public function reset_via_email() {
        $this->load->model('common_model');
        $this->load->helper('cookie');
        $redata = $this->httpcode;
        $email = $this->input->post('email');
        $verifycode = $this->input->post('verifycode');
        $isemail = verify_email($email);
        $isset = $this->common_model->getList('su_user', ['email' => $email]);
        if ($isemail && is_numeric($verifycode)) {
            if (!empty($isset)) {
                $cookie = get_cookie($email);
                if ($verifycode == $cookie) {
                    $reset = $this->common_model->updateItem('su_user', ['email' => $email], ['password' => md5('su' . 'a123456')]);
                    if ($reset) {
                        $redata['errno'] = 666;
                        $redata['error'] = '密码已经重置为[ a123456 ],请登录后及时修改';
                    } else {
                        $redata['errno'] = 603;
                        $redata['error'] = '密码重置失败,请稍后重试';
                    }
                } else {
                    $redata['errno'] = 602;
                    $redata['error'] = '验证码错误';
                }
            } else {
                $redata['errno'] = 601;
                $redata['error'] = '该邮箱还没有注册';
            }
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '数据格式错误';
        }
        echo json_encode($redata);
    }
    public function initdata(){
        $this->load->model('common_model');
        /*
         * 每日凌晨更新数据库数据
         * su_sender [today_usage enable]
         * su_receiver [today_usage enable]
         * su_mailserver [today_usage enable]
         */
        $initdata_user = [
            'today_usage'=>0
        ];
        $data = [
            'today_usage'=>0,
            'enable'=>1
        ];
        $state_user = $this->common_model->updateItem('su_user',[],$initdata_user);
        $state_sender = $this->common_model->updateItem('su_sender',[],$data);
        $state_receiver = $this->common_model->updateItem('su_receiver',[],$data);
        $state_mailserver = $this->common_model->updateItem('su_mailserver',[],$data);
        if($state_user&&$state_sender&&$state_receiver&&$state_mailserver){
            echo '数据重置成功!';
        }
    }
    

}
